// pages/operation/class/classOrder/index.js
import { ClassModel } from "../../../../api/api-work/classModel.js"
const classModel = new ClassModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isBuKe: false, // 是不是补课过来的
    id: "",
    orderNo: "", // 传值用  上一页过来去下一页
    idsStr: "", // 传值用  上一页过来去下一页
    cofId: "", // 传值用  上一页过来去下一页
    classDateStr: "", // 传值用  上一页过来去下一页
    classId: "",

    classDate: "", // 日期
    classDateName: "", // 日期显示 月日
    weekName: "", // 星期
    classStartTime: "", // 开始时间
    classEndTime: "", // 结束时间
    className: "", // 班级
    teacherName: "", // 老师
    classRoomName: "", // 教室
    classHour: 1, // 授课课时
    studentTableDatas: [],
    count1: 0, // 到课人数
    count2: 0, // 迟到人数
    count3: 0, // 请假人数
    count4: 0, // 未到人数

    showClassHour: false,
    // 单个学员扣课时
    classHourObj: {
      id: "",
      studentName: "", // 学生姓名
      courseName: "", // 授课名称
      num: "", // 剩余课时
      fishNum: "", // 
      beyongNum: "", // 超上课时
      classHourPay: "", // 本次扣课时
      description: "", // 备注
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {

    if (options.param) {
      // 补课过来的
      this.setData({
        isBuKe: true
      })
      const param = JSON.parse(options.param)
      const weekDays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
      const date = new Date(param.classDate);
      const weekday = date.getDay();

      param.selectStudents.forEach(item => {
        item.state = '2'; // 开班补课默认点名到课
        item.classHourPay = param.classHour;
        item['isBeyong'] = parseFloat(item.num) < 0;
        item['fishNum'] = parseFloat(item.num) < 0 ? '0' : item.num;
        item['beyongNum'] = Math.abs(parseFloat(item.num));
      })
      this.setData({
        id: param.courseId,
        classId: param.classId,
        classDate: param.classDate,
        classDateName: param.classDate.split("-")[1] + "月" + param.classDate.split("-")[2] + "日",
        weekName: weekDays[weekday],
        classStartTime: param.classStartTime,
        classEndTime: param.classEndTime,
        className: param.className,
        teacherName: param.teacherName,
        teacher: param.teacher,
        classRoomName: param.classRoomName,
        classHour: param.classHour,
        classHourPay: param.classHour,
        studentTableDatas: param.selectStudents,
      })
      // 统计人数
      this.countNum(this.data.studentTableDatas);
    }
    else {
      this.setData({
        isBuKe: false
      })
      this.setData({
        id: options.id,
        orderNo: options.orderNo,
        idsStr: options.idsStr,
        cofId: options.cofId,
        classDateStr: options.classDate
      })
      await this.getClassTimeById()
      await this.getClassTimeStudentList()
    }


  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (this.data.isBuKe) {
      return;
    }
    this.getClassTimeStudentList()
    this.getClassTimeById()
  },

  // 获取点名信息
  getClassTimeById() {
    classModel.getClassTimeByIdApi({ id: this.data.id }).then(res => {
      const weekDays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
      const date = new Date(res.classDate.split(" ")[0]);
      const weekday = date.getDay();

      this.setData({
        classDateName: res.classDate.split(" ")[0].split("-")[1] + "月" + res.classDate.split(" ")[0].split("-")[2] + "日",
        weekName: weekDays[weekday],
        classEndTime: res.classEndTime,
        classStartTime: res.classStartTime,
        className: res.className,
        teacherName: res.teacherName,
        classRoomName: res.classRoomName,
        classHour: res.classHour,
      })
    })
  },

  // 获取学员信息
  getClassTimeStudentList() {
    classModel.getClassTimeStudentListApi({ orderNo: this.data.orderNo, pageNum: 1, pageSize: 999 }).then(res => {
      res.rows.forEach(item => {
        item.state = item.state == '1' ? '2' : item.state;
        item.classHourPay = this.data.classHour;
        item['isBeyong'] = parseFloat(item.num) < 0;
        item['fishNum'] = parseFloat(item.num) < 0 ? '0' : item.num;
        item['beyongNum'] = Math.abs(parseFloat(item.num));
      })
      this.setData({
        studentTableDatas: res.rows
      })
      // 统计人数
      this.countNum(this.data.studentTableDatas);
    })
  },

  // 更改授课课时
  onStepChange(e) {
    this.data.studentTableDatas.forEach(item => {
      if (item.state == '4' || item.state == '5') {
        // 请假，未到的  不扣课时
        item.classHourPay = 0
      }
      else {
        item.classHourPay = e.detail
      }
    })
    this.setData({
      classHour: e.detail,
      studentTableDatas: this.data.studentTableDatas
    })
  },

  // 更改点名状态
  onRadioChange(e) {
    this.data.studentTableDatas.forEach(item => {
      if (item.studentId == e.currentTarget.dataset.query.studentId) {
        item.state = e.detail;
        if (item.state == '4' || item.state == '5') {
          // 请假 未到  不扣课时
          item.classHourPay = 0;
        }
        else {
          item.classHourPay = this.data.classHour
        }
      }
    })
    this.setData({
      studentTableDatas: this.data.studentTableDatas
    })
    // 统计人数
    this.countNum(this.data.studentTableDatas);
  },

  // 统计人数
  countNum(datas) {
    this.setData({
      count1: 0,
      count2: 0,
      count3: 0,
      count4: 0
    })
    if (datas.length > 0) {
      datas.forEach(item => {
        if (item.state == '2') {
          // 到课
          this.setData({
            count1: this.data.count1 + 1
          })
        }
        else if (item.state == '3') {
          // 迟到
          this.setData({
            count2: this.data.count2 + 1
          })
        }
        else if (item.state == '4') {
          // 请假
          this.setData({
            count3: this.data.count3 + 1
          })
        }
        else {
          // 未到
          this.setData({
            count4: this.data.count4 + 1
          })
        }
      })
    }
  },

  // 完成点名
  confirmStudent() {
    // 调用完成点名接口

    if (this.data.isBuKe) {
      // 开班补课的
      const param = {
        classDate: this.data.classDate + " 00:00:00",
        classEndTime: this.data.classEndTime,
        classStartTime: this.data.classStartTime,
        classHour: this.data.classHourPay,
        classId: this.data.classId,
        courseId: this.data.id,
        teacher: this.data.teacher,
        immediately: '0',
        tCourseOrders: this.data.studentTableDatas
      }

      classModel.courseOrderSaveRepairApi(param).then(res => {
        wx.navigateBack({
          delta: 4
        })
      })
    }
    else {
      classModel.updateBatchOrderApi(this.data.studentTableDatas).then(res => {
        wx.navigateBack()
      })
    }

  },

  // 添加临时学员
  addTempStudent() {
    wx.navigateTo({
      url: `/pages/operation/class/addTempStudent/index?orderNo=${this.data.orderNo}&idsStr=${this.data.idsStr}&cofId=${this.data.cofId}&classDate=${this.data.classDateStr}`,
    })
  },

  // 修改课次信息
  gotoClassCourseEdit() {
    wx.navigateTo({
      url: `/pages/operation/class/classCourseEdit/index?id=${this.data.id}&orderNo=${this.data.orderNo}`,
    })
  },

  // 单独修改课时
  changeClassHour(e) {

    const val = e.currentTarget.dataset.query;
    console.log("val: ", val)
    this.setData({
      showClassHour: true,
      "classHourObj.id": val.id,
      "classHourObj.corseName": val.courseName,
      "classHourObj.num": val.num,
      "classHourObj.fishNum": val.fishNum,
      "classHourObj.beyongNum": val.beyongNum,
      "classHourObj.classHourPay": val.classHourPay,
      "classHourObj.description": val.description,
      "classHourObj.studentName": val.studentName,
    })
  },

  onCloseHour() {
    this.setData({
      showClassHour: false
    })
  },

  onStepHourChange(e) {
    this.setData({
      "classHourObj.classHourPay": e.detail,
    })
  },

  onDesChange(e) {
    this.setData({
      "classHourObj.description": e.detail,
    })
  },

  // 确认修改学员课时
  confirmStudentHour() {

    this.data.studentTableDatas.forEach(item => {
      if (item.id == this.data.classHourObj.id) {
        item.classHourPay = this.data.classHourObj.classHourPay;
        item.description = this.data.classHourObj.description;
      }
    })
    this.setData({
      showClassHour: false,
      studentTableDatas: this.data.studentTableDatas
    })
  },

  // 移除课次
  removeStudentHour() {
    classModel.deleteClassTimeApi({ id: this.data.classHourObj.id }).then(res => {
      this.setData({
        showClassHour: false,
      })
      this.getClassTimeStudentList()
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})